import tempfile
import unittest
import uuid
from pathlib import Path

from yt_dlp import YoutubeDL

from video_summarize_tools.tools import video_web_page_parser
from video_summarize_tools.tools.video_web_page_parser import TargetVideoFileClarity


class VideoWebPageParserTest(unittest.TestCase):
    def test_1(self):
        download_path = Path(tempfile.gettempdir()) / str(uuid.uuid4())
        ydl_opts = {
            'outtmpl': str(download_path / '%(title)s.f%(format_id)s.%(ext)s'),
            'cookiesfrombrowser': ('firefox',)  # 这里要做兼容处理
        }

        with YoutubeDL(ydl_opts) as ydl:
            i = ydl.extract_info("https://www.youtube.com/watch?v=xygyHt4Z45c", download=False)
            exact_file_name = ydl.prepare_filename(i)
            print(exact_file_name)

    def test_download_from_youtube(self):
        url = "https://www.youtube.com/watch?v=xygyHt4Z45c"
        result_path = video_web_page_parser.download_from_youtube( url, TargetVideoFileClarity.BEST)
        print(result_path)